package com.example.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.Base.BasePersonActivity;
import com.example.Fragment.DoctorFragment;
import com.example.Fragment.ProductionOnMyPostFragment;
import com.example.Fragment.TopicOnMyPostFragment;
import com.example.adapter.MyFragmentPagerAdapter;
import com.example.administrator.olddoctor.R;

import java.util.ArrayList;

/**
 * 我的帖子
 */
public class MyPostActivity extends BasePersonActivity implements View.OnClickListener{

    private ViewPager mviewPager;
    private TextView production, topic;
    private ImageView ivBottomLine;

    private int currIndex = 0;
    private int bottomLineWidth;
    private int offset = 0;
    private int position_one;
    public final static int num = 2 ;

    private ArrayList<Fragment> fragmentsList;
    private ProductionOnMyPostFragment postFragment;
    private TopicOnMyPostFragment topicFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mypost);
        init();
        calculate();
        initpager();
    }

    private void init() {
        setActionBar(getResources().getDrawable(R.mipmap.call_back), null,"我的帖子",null,null,"编辑");
        ivBottomLine = (ImageView)findViewById(R.id.ivBottomLine);
        production = (TextView) findViewById(R.id.production);//作品
        topic = (TextView) findViewById(R.id.topic);//话题
        mviewPager = (ViewPager) findViewById(R.id.mviewPager);
        production.setOnClickListener(this);
        topic.setOnClickListener(this);
    }

    public void calculate (){
        bottomLineWidth = ivBottomLine.getLayoutParams().width;
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;
        offset = (int) ((screenW / num - bottomLineWidth) / 2);
        int avg = (int) (screenW / num);
        position_one = avg + offset;
        settingLineview();
    }

    private void initpager() {
        fragmentsList = new ArrayList<Fragment>();
        postFragment = new ProductionOnMyPostFragment();
        topicFragment = new TopicOnMyPostFragment();
        fragmentsList.add(postFragment);
        fragmentsList.add(topicFragment);
        mviewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList));
        mviewPager.setOnPageChangeListener(new MyPageChangeListener());
        mviewPager.setCurrentItem(0);
    }

    public void settingLineview(){
        TranslateAnimation animation = new TranslateAnimation(0, offset, 0, 0);
        topic.setTextColor(getResources().getColor(R.color.white));
        animation.setFillAfter(true);
        animation.setDuration(0);
        ivBottomLine.startAnimation(animation);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.production://作品
                mviewPager.setCurrentItem(0);
                break;
            case R.id.topic://话题
                mviewPager.setCurrentItem(1);
                break;
        }
    }

    public class MyPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageSelected(int arg0) {
            Animation animation = null;
            switch (arg0) {
                case 0:
                    if (currIndex == 1) {
                        animation = new TranslateAnimation(position_one, offset, 0, 0);
                        topic.setTextColor(getResources().getColor(R.color.white));
                    }
                    production.setTextColor(getResources().getColor(R.color.top_bg));
                    break;
                case 1:
                    if (currIndex == 0) {
                        animation = new TranslateAnimation(offset, position_one, 0, 0);
                        production.setTextColor(getResources().getColor(R.color.white));
                    }
                    topic.setTextColor(getResources().getColor(R.color.top_bg));
                    break;
            }
            currIndex = arg0;
            animation.setFillAfter(true);
            animation.setDuration(300);
            ivBottomLine.startAnimation(animation);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {}

        @Override
        public void onPageScrollStateChanged(int arg0) {}
    }
}
